package com.example.sunmiv1.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.ReportLossListInfo;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReportLossManageAdapter extends BaseAdapter {

    private Context context;
    private List<ReportLossListInfo.DataBean> list;
    private ViewHolder viewHolder;
    private Operation operation;
    private ACache aCache;

    public ReportLossManageAdapter(Context context, List<ReportLossListInfo.DataBean> list) {
        this.context = context;
        this.list = list;
        aCache = ACache.get(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = View.inflate(context, R.layout.report_loss_manage_adapter, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.rlmAdapterPeople.setText(list.get(i).getOperator());
        viewHolder.rlmAdapterTime.setText(list.get(i).getAdd_time());
        viewHolder.rlmAdapterOrderNo.setText(list.get(i).getOrder_number());
        if("2".equals(aCache.getAsString("userType"))&&"0".equals(aCache.getAsString("showPrice"))){
            viewHolder.rlmAdapterNumberLin.setVisibility(View.GONE);
        }else{
            viewHolder.rlmAdapterNumberLin.setVisibility(View.VISIBLE);
        }
        viewHolder.rlmAdapterNumber.setText("$" + list.get(i).getMoney_total());
        viewHolder.rlmAdapterWarehouse.setText(list.get(i).getWarehouse_name());
        viewHolder.rlmAdapterRemark.setText(list.get(i).getRemarks());
        final int index = i;
        viewHolder.rlmAdapterDetail.setOnClickListener(view1 -> operation.success(index));
        return view;
    }

    public void Click(Operation operation) {
        this.operation = operation;
    }

    public interface Operation {
        void success(int index);
    }

    static class ViewHolder {
        @BindView(R.id.rlmAdapter_people)
        TextView rlmAdapterPeople;
        @BindView(R.id.rlmAdapter_time)
        TextView rlmAdapterTime;
        @BindView(R.id.rlmAdapter_orderNo)
        TextView rlmAdapterOrderNo;
        @BindView(R.id.rlmAdapter_numberLin)
        LinearLayout rlmAdapterNumberLin;
        @BindView(R.id.rlmAdapter_number)
        TextView rlmAdapterNumber;
        @BindView(R.id.rlmAdapter_warehouse)
        TextView rlmAdapterWarehouse;
        @BindView(R.id.rlmAdapter_remark)
        TextView rlmAdapterRemark;
        @BindView(R.id.rlmAdapter_detail)
        TextView rlmAdapterDetail;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
